<script setup>
import { RouterLink, RouterView } from 'vue-router'
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
// 修改图标导入方式
import { Expand, Fold } from '@element-plus/icons-vue'

const router = useRouter()
const isCollapse = ref(false)

// 获取路由配置
const routes = computed(() => {
  return router.options.routes.map(route => ({
    path: route.path,
    name: route.name,
    title: route.title,
    children: route.children || []
  }))
})
</script>

<template>
  <el-container class="layout-container">
    <el-header>Header</el-header>
    <el-container>
      <el-container>
        <el-aside width="200px">
          <el-menu default-active="/" class="el-menu-vertical" :collapse="isCollapse" router>
            <template v-for="route in routes" :key="route.path">
              <!-- 没有子路由的菜单项 -->
              <el-menu-item v-if="!route.children?.length" :index="route.path">
                <el-icon><i-ep-home v-if="route.path === '/'" /><i-ep-video-camera v-else /></el-icon>
                <template #title>{{ route.title }}</template>
              </el-menu-item>
              
              <!-- 有子路由的菜单项 -->
              <el-sub-menu v-else :index="route.path">
                <template #title>
                  <el-icon><i-ep-home v-if="route.path === '/'" /><i-ep-video-camera v-else /></el-icon>
                  <span>{{ route.title }}</span>
                </template>
                <el-menu-item 
                  v-for="child in route.children" 
                  :key="child.path" 
                  :index="child.path"
                >
                  {{ child.title }}
                </el-menu-item>
              </el-sub-menu>
            </template>
          </el-menu>
        </el-aside>
        <el-main style="overflow-y: auto; height: calc(100vh - 120px);">
          <router-view></router-view>
        </el-main>
      </el-container>
      <el-footer>Footer</el-footer>
    </el-container>
  </el-container>
</template>

<style scoped>
.layout-container {
  height: 100%;
}

.el-header,
.el-footer {
  height: 60px;
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
}

.el-menu-vertical {
  height: 100%;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
}
</style>